<?php
/**
 * user: daishiqi
 * Date：2021/6/17
 * Time: 12:16 下午
 */

namespace App\Http\Model;


use Illuminate\Database\Eloquent\Model;

class Agreement extends Model
{
    /**
     * @param $lastVersion
     * @param $userId
     * @return bool
     * @desc 获取用户是否需要同意协议
     */
    public static function getIsNeedAgree($lastVersion, $userId)
    {
        $data = self::query()->where(['user_id' => $userId, 'version' => $lastVersion])->first();
        if ($data) {
            return false;
        }
        return true;

    }

    /**
     * @param $version
     * @param $userId
     * @return \Illuminate\Database\Eloquent\Builder|Model
     * @desc 用户同意协议
     */
    public static function userAgree($version, $userId)
    {
        $where = ['user_id' => $userId, 'version' => $version];
        $values = array_merge($where, ['updated_at' => date('Y-m-d H:i:s'), 'created_at' => date('Y-m-d H:i:s')]);
        return self::query()->updateOrCreate(['user_id' => $userId, 'version' => $version], $values);

    }

}